package cn.itcast.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.itcast.dao.RoleMenuDao;
import cn.itcast.service.RoleMenuService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class RoleMenuServiceImpl implements RoleMenuService {

    @Autowired
    private RoleMenuDao roleMenuDao;

    @Override
    public void updateAll(String roleId, String[] menuId) {
        roleMenuDao.updateAll(roleId,menuId);
    }

    @Override
    public void insertAll(String permissionIds, String[] menuId) {
        roleMenuDao.insertAll(permissionIds,menuId);
    }

    @Override
    public void deleteAll(String roleId, String[] menuId) {
        roleMenuDao.deleteAll(roleId,menuId);
    }

    @Override
    public String[] selectAll(String roleId) {
        return roleMenuDao.selectAll(roleId);
    }

    @Override
    public Integer findByRoleId(String roleId, String menuId) {
        return roleMenuDao.findByRoleId(roleId,menuId);
    }

    @Override
    public void saveAll(String permissionIds, String[] split) {

        String[] delete = selectAll(permissionIds);
        if(ObjectUtil.isNotEmpty(delete)){
            //将自己所有的权限删除
            deleteAll(permissionIds,delete);
        }

        //重新更新自己的权限
        if(ObjectUtil.isNotEmpty(split)){
            insertAll(permissionIds,split);
        }
    }
}
